package com.merge.sample.model;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

import com.merge.sample.util.MyUtil;

public class MData {
	public MData parent;
	public String source;
	public Boolean isMergeable;

	public List<Incluable> inclusives = new ArrayList<Incluable>();

	public MData(MData parent, String source) {
		this.parent = parent;
		this.source = source;
		isMergeable = false;
	}
	
	/**
	 * 
	 * @param str
	 * @return true if exists conflict, or false
	 */
	public boolean isIgnore(String str) {
		boolean rt = false;
		for(Incluable i : inclusives) {
			if(!i.isInclusive) {
				if(i.useRegx) {
					Pattern p = (Pattern) i.value;
					if(p.matcher(str).matches())
						return true;
				} else {
					if(str.equals(i.value))
						return true;
				}
			}
			
		}
		return rt;
	}
	
	public String toString() {
		String rt = (parent == null) ? "" : parent.toString();
		rt += source + ((this instanceof MFile) ? "" : "/");
		if (isMergeable) {
			if (inclusives.size() > 0) {
				rt += " [inclusive: ";
				for (Incluable i : inclusives) {
					rt += i + ",";
				}
				rt += "]";
			}
		}
		return rt;
	}
	
	

	public String getRelativePath() {
		if (this instanceof MProject) {
			return "";
		}
		String psource = (parent == null || parent instanceof MProject) ? ""
				: parent.getRelativePath();

		return psource + File.separator + this.source;
	}

	public MProject getProject() {
		if (this instanceof MProject) {
			return (MProject) this;
		}
		MData tmp = this.parent;
		while (!(tmp instanceof MProject) && tmp != null) {
			tmp = tmp.parent;
		}
		if (tmp instanceof MProject) {
			return (MProject) tmp;
		}
		return null;
	}
}
